package project.client;

import project.common.Request;
import project.common.Response;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

/**
 * @author oldliu
 * @since 1.0
 */
public class MainClient {
    private static ObjectOutputStream oout;
    private static ObjectInputStream oin;

    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        Socket s = new Socket("localhost", 9999);
        InputStream in = s.getInputStream();
        OutputStream out = s.getOutputStream();
        //用对象
        oout = new ObjectOutputStream(out);
        oin = new ObjectInputStream(in);
        int ch = 0;
        while (true) {
            System.out.println("1：登录，2：上传 3:注册,9：退出");
            ch = sc.nextInt();
            if (ch == 1) {
                Request request = new Request("login");
                System.out.println("请输入用户名:");
                request.put("userName", sc.next());
                System.out.println("请输入密码:");
                request.put("userPass", sc.next());

                oout.writeObject(request);
                oout.reset();
                Response response = (Response) oin.readObject();
                if (response.isSuccess()) {
                    System.out.println("登录成功");
                } else {
                    System.out.println("shibai:" + response.getMsg());
                }
            } else if (ch == 2) {
                System.out.println("请输入文件路径:");
                String fileName = sc.next();
                File f = new File(fileName);
                Request request = new Request("upload");
                request.put("fileName", f.getName());
                FileInputStream fin = new FileInputStream(f);
                byte[] b = new byte[fin.available()];
                fin.read(b);//length==b.length
                fin.close();
                request.put("content", b);
                oout.writeObject(request);
                oout.reset();
                Response response = (Response) oin.readObject();
                System.out.println("上传的结果是：" + response.isSuccess() + ",msg:" + response.getMsg());
            }else if(ch==3){
                System.out.println("注册:");
                Request request = new Request("register");
                System.out.println("请输入用户名:");
                request.put("name", sc.next());
                System.out.println("请输入密码:");
                request.put("pass", sc.next());
                oout.writeObject(request);
                oout.reset();
                Response response = (Response) oin.readObject();
                if (response.isSuccess()) {
                    System.out.println("注册成功");
                } else {
                    System.out.println("shibai:" + response.getMsg());
                }


            }else if(ch==9){
                //告别。。。
                s.close();
                break;
            }


        }
    }
}
